using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace BodySoundSimulator
{
    class SupportiveFunctions
    {
        public static void SerializeString(BinaryWriter writer, string str)
        {
            UnicodeEncoding encoding = new UnicodeEncoding();
            byte[] bytes = encoding.GetBytes(str.ToCharArray());
            int length = bytes.Length;
            writer.Write(length);
            for (int index = 0; index < length; index++)
            {
                writer.Write(bytes[index]);
            }
        }

        public static string DeserializeString(BinaryReader reader)
        {
            int length = reader.ReadInt32();
            byte[] bytes = new byte[length];
            for (int index = 0; index < length; index++)
            {
                bytes[index] = reader.ReadByte();
            }
            UnicodeEncoding encoding = new UnicodeEncoding();
            return encoding.GetString(bytes);
        }
    }
}
